package com.qfedu.service.impl;

import com.qfedu.dao.CartDao;
import com.qfedu.dao.impl.CartDaoImpl;
import com.qfedu.pojo.Cart;
import com.qfedu.pojo.Goods;
import com.qfedu.service.CartService;
import com.qfedu.service.GoodsService;

import java.util.List;

/**
 * 创建者: QF-SUN
 * 创建时间: 2023/4/4 15:07
 * 描述:
 */
public class CartServiceImpl implements CartService {

    CartDao cartDao = new CartDaoImpl();

    GoodsService goodsService = new GoodsServiceImpl();

    @Override
    public Cart findCart(int uid, String goodsId) {
        return cartDao.findCart(uid, goodsId);
    }

    @Override
    public void insert(Cart cart) {
        cartDao.insert(cart);
    }

    @Override
    public void update(Cart cart) {
        cartDao.update(cart);
    }

    /**
     * //查询购物车，且需要包含商品对象
     * @param uid
     * @return
     */
    @Override
    public List<Cart> selectCart(int uid) {
        List<Cart> carts = cartDao.findCartByUId(uid);
        //循环 购物车， 根据商品id 查询商品对象， 存储到购物车
        if(carts != null && carts.size() > 0) {
            for (Cart cart : carts) {
                int goodsId = cart.getPid();
                Goods goods = goodsService.findById(goodsId);
                cart.setGoods(goods);
            }
        }
        return carts;
    }

    @Override
    public void deleteCart(int uid, String goodsId) {
        cartDao.deleteCart(uid, goodsId);
    }

    @Override
    public void clearCart(int id) {
        cartDao.clearCart(id);
    }
}
